package com.softwinner.bt;

import com.softwinner.bt.util.BTConfig;
import com.softwinner.bt.util.BTLog;
import com.softwinner.bt.util.Command;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.util.Log;
public class InCallDialog extends BaseActivity {
    private ImageView handupButton;
    private ImageView keyButton = null;
    private Button keyNum0;
    private Button keyNum1;
    private Button keyNum2;
    private Button keyNum3;
    private Button keyNum4;
    private Button keyNum5;
    private Button keyNum6;
    private Button keyNum7;
    private Button keyNum8;
    private Button keyNum9;
    private Button keyNump;
    private Button keyNumx;
    private View mKeyboardContainer = null;
    private String phoneName;
    private TextView phoneNameV;
    private String phoneNum = null;
    private TextView phoneNumV;
    private ImageView soundAddButton;
    private ImageView soundDecButton;
    private TextView timeText;
    private final static int UPDATE_TIME = 0;
    public static boolean isCall = false;
    private Handler mHandler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
            case UPDATE_TIME:
                updateTime();
                break;

            default:
                break;
            }
        }
    };
    private StringBuffer mBuffer;
    private int mCurrentTime = 0;

    private void updateTime() {
        String time = "";
        int h = mCurrentTime / 3600;
        int m = mCurrentTime % 3600 / 60;
        int s = mCurrentTime % 60;
        time = "" + (h > 9 ? h : "0" + h) + ":" + (m > 9 ? m : "0" + m) + ":"
                + (s > 9 ? s : "0" + s);
        timeText.setText("" + time);
        mHandler.sendEmptyMessageDelayed(UPDATE_TIME, 1000);
        mCurrentTime++;
    }

    protected void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
        setContentView(R.layout.bt_incall);
        mBTManager.sendCommand(Command.SEND_QUERY_VOL);
        
        isCall=true;
        
        Bundle localBundle = getIntent().getExtras();
        mBuffer = new StringBuffer();
        this.phoneNum = localBundle.getString("phoneNum");
        try {
        	this.phoneName = getName(this.phoneNum);
		} catch (Exception e) {}
        this.phoneNameV = ((TextView) findViewById(R.id.bt_incall_name));
        this.phoneNumV = ((TextView) findViewById(R.id.bt_incall_num));
        this.phoneNameV.setText(this.phoneName);
        this.phoneNumV.setText(this.phoneNum);
        this.timeText = ((TextView) findViewById(R.id.bt_incall_time));
        this.mKeyboardContainer = (View) findViewById(R.id.incall_keyboard_container);
        this.keyNum0 = ((Button) findViewById(R.id.incall_num0));
        this.keyNum1 = ((Button) findViewById(R.id.incall_num1));
        this.keyNum2 = ((Button) findViewById(R.id.incall_num2));
        this.keyNum3 = ((Button) findViewById(R.id.incall_num3));
        this.keyNum4 = ((Button) findViewById(R.id.incall_num4));
        this.keyNum5 = ((Button) findViewById(R.id.incall_num5));
        this.keyNum6 = ((Button) findViewById(R.id.incall_num6));
        this.keyNum7 = ((Button) findViewById(R.id.incall_num7));
        this.keyNum8 = ((Button) findViewById(R.id.incall_num8));
        this.keyNum9 = ((Button) findViewById(R.id.incall_num9));
        this.keyNumx = ((Button) findViewById(R.id.incall_num_star));
        this.keyNump = ((Button) findViewById(R.id.incall_num_pound));
        this.keyNum0.setOnClickListener(this);
        this.keyNum1.setOnClickListener(this);
        this.keyNum2.setOnClickListener(this);
        this.keyNum3.setOnClickListener(this);
        this.keyNum4.setOnClickListener(this);
        this.keyNum5.setOnClickListener(this);
        this.keyNum6.setOnClickListener(this);
        this.keyNum7.setOnClickListener(this);
        this.keyNum8.setOnClickListener(this);
        this.keyNum9.setOnClickListener(this);
        this.keyNumx.setOnClickListener(this);
        this.keyNump.setOnClickListener(this);
        this.keyButton = ((ImageView) findViewById(R.id.incall_keyboard));
        this.keyButton.setOnClickListener(this);
        this.handupButton = ((ImageView) findViewById(R.id.incall_hangup));
        this.handupButton.setOnClickListener(this);
        this.soundAddButton = ((ImageView) findViewById(R.id.incall_vol_up));
        this.soundAddButton.setOnClickListener(this);
        this.soundDecButton = ((ImageView) findViewById(R.id.incall_vol_down));
        this.soundDecButton.setOnClickListener(this);        
        updateTime();
    }
    
    public BroadcastReceiver receiver = new BroadcastReceiver() {
		
		@Override
		public void onReceive(Context arg0, Intent intent) {
			// TODO Auto-generated method stub
			String action = intent.getAction();
			int cmd = intent.getExtras().getInt(BTConfig.ECAR_SENDCMD);
			if(BTConfig.ECAR_SEND_ACTION.equals(action)){				
				if(cmd == BTConfig.HAND_UP_CMD){
					mBTManager.sendCommand(Command.SEND_HANG_UP);
					finish();
				}
			}
		}
	};

    protected void onDestroy() {
		BTLog.v(this.getClass(), "onDestroy()");
		isCall = false;
        super.onDestroy();
        mCurrentTime = 0;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.incall_hangup:
            mBTManager.sendCommand(Command.SEND_HANG_UP);
            finish();
            break;
        case R.id.incall_vol_up:
            mBTManager.sendCommand(Command.SEND_VOLUME_UP);
            break;
        case R.id.incall_vol_down:
            mBTManager.sendCommand(Command.SEND_VOLUME_DOWN);
            break;
        case R.id.incall_keyboard:
            if (mKeyboardContainer.getVisibility() == View.VISIBLE) {
            	mKeyboardContainer.setVisibility(View.INVISIBLE);
            } else {
            	mKeyboardContainer.setVisibility(View.VISIBLE);
            }
            break;
        case R.id.incall_num0:
            mBTManager.sendCommand(Command.SEND_SUB_CALL + "0");
            mBuffer.append("0");
            phoneNumV.setText(mBuffer.toString());
            break;
        case R.id.incall_num1:
            mBTManager.sendCommand(Command.SEND_SUB_CALL + "1");
            mBuffer.append("1");
            phoneNumV.setText(mBuffer.toString());
            break;
        case R.id.incall_num2:
            mBTManager.sendCommand(Command.SEND_SUB_CALL + "2");
            mBuffer.append("2");
            phoneNumV.setText(mBuffer.toString());
            break;
        case R.id.incall_num3:
            mBTManager.sendCommand(Command.SEND_SUB_CALL + "3");
            mBuffer.append("3");
            phoneNumV.setText(mBuffer.toString());
            break;
        case R.id.incall_num4:
            mBTManager.sendCommand(Command.SEND_SUB_CALL + "4");
            mBuffer.append("4");
            phoneNumV.setText(mBuffer.toString());
            break;
        case R.id.incall_num5:
            mBTManager.sendCommand(Command.SEND_SUB_CALL + "5");
            mBuffer.append("5");
            phoneNumV.setText(mBuffer.toString());
            break;
        case R.id.incall_num6:
            mBTManager.sendCommand(Command.SEND_SUB_CALL + "6");
            mBuffer.append("6");
            phoneNumV.setText(mBuffer.toString());
            break;
        case R.id.incall_num7:
            mBTManager.sendCommand(Command.SEND_SUB_CALL + "7");
            mBuffer.append("7");
            phoneNumV.setText(mBuffer.toString());
            break;
        case R.id.incall_num8:
            mBTManager.sendCommand(Command.SEND_SUB_CALL + "8");
            mBuffer.append("8");
            phoneNumV.setText(mBuffer.toString());
            break;
        case R.id.incall_num9:
            mBTManager.sendCommand(Command.SEND_SUB_CALL + "9");
            mBuffer.append("9");
            phoneNumV.setText(mBuffer.toString());
            break;
        case R.id.incall_num_pound:
            mBTManager.sendCommand(Command.SEND_SUB_CALL + "#");
            mBuffer.append("#");
            phoneNumV.setText(mBuffer.toString());
            break;
        case R.id.incall_num_star:
            mBTManager.sendCommand(Command.SEND_SUB_CALL + "*");
            mBuffer.append("*");
            phoneNumV.setText(mBuffer.toString());
            break;
        default:
            break;
        }
    }

	@Override
	protected void phoneStateChanged(String action) {
		// TODO Auto-generated method stub
		
	}
}
